package com.ocom.mqsimple.business.recharge;

import com.alibaba.fastjson.JSON;
import com.ocom.common.request.simple.RechargeRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.vo.human.UserInfoYkmVo;
import com.ocom.common.vo.pay.UpdateListRechargeVo;
import com.ocom.mqsimple.config.RechargeConfig;
import com.ocom.mqsimple.feign.HumanClientService;
import com.ocom.mqsimple.feign.PayConfigClientService;
import com.ocom.redis.util.RedisUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class RechargeConsumerMqListener {

	private Logger logger = LoggerFactory.getLogger(RechargeConsumerMqListener.class);

	@Resource
	private RedisUtil redisUtil;

	@Autowired
	private HumanClientService humanClientService;

	@Autowired
	private PayConfigClientService payConfigClientService;


	/**
	 * 把该监听命名为A监听
	 * 监听队列  远程服务支付接口发送消息  该队列会接受
	 * [message, headers, channel]
	 * @return {@link }
	 * @throws
	 */
	@RabbitListener(queues = RechargeConfig.RECHARGE_QUEUE)
	public void process(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
		logger.info("Recharge 消费中");
		logger.info("message【{}】",message);
		String humanMessage = new String(message.getBody(), StandardCharsets.UTF_8);
		//TODO 数据库操作样例
		logger.info("Recharge-start:【{}】",humanMessage);
//		logger.info("System.currentTimeMillis()【{}】",System.currentTimeMillis());


		RechargeRequest rechargeRequest = JSON.parseObject(humanMessage, RechargeRequest.class);
		Map<String,Object> map3=new HashMap<>();
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("comId",rechargeRequest.getComId());
//			map.put("pName",rechargeRequest.getPName());
			map.put("pCode",rechargeRequest.getPCode());
			map.put("page",1);
			map.put("size",1);
			Result result = humanClientService.PersonnelGetPersonInfo(map);

			if (result.getCode()!=200){
				throw new RuntimeException("人员未找到");
			}
			String list = JSON.parseObject(JSON.toJSONString(result.getData())).get("list").toString();
			List<UserInfoYkmVo> userInfoYkmVos = JSON.parseArray(list, UserInfoYkmVo.class);
			Map<String, Object> map1 = new HashMap<>();
			if (userInfoYkmVos.size()>0) {
				UserInfoYkmVo userInfoYkmVo = userInfoYkmVos.get(0);

				UpdateListRechargeVo updateListRechargeVo =new UpdateListRechargeVo();
				updateListRechargeVo.setComId(rechargeRequest.getComId());
				updateListRechargeVo.setPId(userInfoYkmVo.getPId());
				updateListRechargeVo.setRechargeMoney(rechargeRequest.getMoney());
				updateListRechargeVo.setSubAccount(rechargeRequest.getSubAccount());

				Result result1 = payConfigClientService.UpdateListRecharge(updateListRechargeVo);
				if(result1.getCode()!=200){
					map3.put("errMsg","充值失败:"+result1.getMsg());
					map3.put("json",rechargeRequest);
					redisUtil.leftPush("ImportErr:detail:"+rechargeRequest.getComId()+":"+rechargeRequest.getTimestamp(),map3,86400l);
				}
			}else{
				throw new RuntimeException("人员未找到");
			}
		} catch (Exception e) {
			logger.info("faceErr-出现异常队列消息开始进入死信队列{{}}",e);

			map3.put("errMsg","充值失败:exception:"+e);
			map3.put("json",rechargeRequest);
			redisUtil.leftPush("ImportErr:detail:"+rechargeRequest.getComId()+":"+rechargeRequest.getTimestamp(),map3,86400l);

		}

		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		logger.info("Recharge-end:【{}】",humanMessage);
//		logger.info("System.currentTimeMillis()【{}】",System.currentTimeMillis());
	}

	/**
	 * 把该监听命名为B监听
	 * 监听死信队列  远程服务支付接口发送消息  该队列会接受
	 * [message, headers, channel]
	 * @return {@link }
	 * @throws
	 */
	@RabbitListener(queues =  RechargeConfig.RECHARGE_DIE_QUEUE)
	public void processForDie(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
		//死信队列放弃
		channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
		logger.info("充值死信队列放弃 ， 【{}】",message);
	}


}
